package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/checkgroup")
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        log.info("[检查组添加]checkGroup:{},checkitenIds:{}",checkGroup,checkitemIds);
        checkGroupService.add(checkGroup,checkitemIds);
        return new Result(true, MessageConst.ADD_CHECKGROUP_SUCCESS);
    }
    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){
        log.info("[检查组分页查询]queryPageBean:{}",queryPageBean);
        PageResult pageResult = checkGroupService.pageQuery(queryPageBean);
        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,pageResult);
    }
    @GetMapping("findById")
    public Result findById(Integer id){
        log.info("[根据id查询检查组]id:{}",id);
        CheckGroup checkGroup = checkGroupService.finfById(id );
        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroup);
    }
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByChechGroupId(Integer id){
        log.info("[根据检查组id查询关联的检查项id集合]id:{}",id);
        List<Integer> itemIds = checkGroupService.findCheckItemIdsByCheckGroupId(id);
        return new Result(true,MessageConst.QUERY_CHECKITEM_SUCCESS,itemIds);
    }
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup, Integer[] checkitemIds) {
        log.info("[检查组-编辑]data:{},checkitemIds:{}", checkGroup, checkitemIds);

        //RPC请求添加
        checkGroupService.edit(checkGroup, checkitemIds);
        return new Result(true, MessageConst.EDIT_CHECKGROUP_SUCCESS);
    }
    @PostMapping("/deleteCheckGroupitemById")
    public Result deleteCheckGroupitemById(@RequestParam("id") Integer id){
        try {
            checkGroupService.delete(id);
            return  new Result(true, MessageConst.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConst.DELETE_CHECKGROUP_FAIL);
        }

    }
    @GetMapping("findAll")
    public Result findAll(){
        List<CheckGroup> checkGroups = checkGroupService.findAll();
        return new Result(true,MessageConst.QUERY_CHECKGROUP_SUCCESS,checkGroups);
    }
}
